class Solution {
public:
    int maxEqualFreq(vector<int>& nums) {
        unordered_map<int,int> hash,sum;
        int ret = 0;
        int Max = 0;
        for(int i = 0;i < nums.size();i++){
            hash[nums[i]]++;
            
            sum[hash[nums[i]]]++;
            sum[hash[nums[i]]-1]--;
            
            Max = max(Max,hash[nums[i]]);
            
            if(Max == 1){
                ret = max(ret,i+1);
            }
            
            if(sum[Max] * Max + 1 == i + 1){
                ret = max(ret,i+1);
            }
            if(sum[Max-1] * (Max-1) + Max == i + 1){
                ret = max(ret,i+1);
            }
        }
        return ret;
    }
};
